package net.oschina.app.adapter.BaseAdapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import net.oschina.app.MApp;
import net.oschina.app.R;
import net.oschina.app.adapter.BaseRecyclerViewAdapter;
import net.oschina.app.bean.Tweet;
import net.oschina.app.bean.TweetsList;
import net.oschina.app.ui.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static net.oschina.app.MApp.mContext;

/**
 * Created by CXB on 2017/4/5.
 */

public class BaseTweetAdapter extends BaseRecyclerViewAdapter {


    private TweetsList tweetsList;
    private List<Tweet> mTweets = new ArrayList<>();

    public BaseTweetAdapter(Context context, TweetsList tweetsList) {
        super(context);
        mTweets = tweetsList.getTweetslist();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.tweet_xr_item, parent, false);
        MyHolder myHolder = new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        MyHolder myHolder = (MyHolder) holder;
        //设置姓名
        myHolder.mItemTvName.setText(mTweets.get(position).getAuthor());
        //设置内容
        Spanned spanned = Html.fromHtml(mTweets.get(position).getBody());
        myHolder.mItemContent.setMovementMethod(LinkMovementMethod.getInstance());
        myHolder.mItemContent.setText(spanned);
        //设置时间
        myHolder.mItemDate.setText(mTweets.get(position).getPubDate());
        //设置头像
        Glide.with(MApp.mContext).load(mTweets.get(position).getPortrait()).fitCenter().into(myHolder.mItemXrSweetIcon);
        //设置内容图片
        Glide.with(MApp.mContext).load(mTweets.get(position).getImgBig()).fitCenter().into(myHolder.mItemContentIv);
        //设置评论数
        myHolder.mCommonCount.setText(mTweets.get(position).getCommentCount());
        //点赞的点击事件
        myHolder.mItemDianzan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "赞一个", Toast.LENGTH_LONG).show();

            }
        });

    }

    @Override
    public int getItemCount() {
        return mTweets.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_xr_sweet_icon)
        CircleImageView mItemXrSweetIcon;
        @BindView(R.id.item_tv_name)
        TextView mItemTvName;
        @BindView(R.id.item_iv_phone)
        ImageView mItemIvPhone;
        @BindView(R.id.item_content)
        TextView mItemContent;
        @BindView(R.id.item_content_iv)
        ImageView mItemContentIv;
        @BindView(R.id.common_count)
        TextView mCommonCount;
        @BindView(R.id.item_date)
        TextView mItemDate;
        @BindView(R.id.item_commont)
        ImageView mItemCommont;
        @BindView(R.id.item_dianzan)
        ImageView mItemDianzan;
        @BindView(R.id.item_btnComment)
        Button mItemBtnComment;
        @BindView(R.id.item_lastLine)
        View mItemLastLine;

        public MyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
